﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
namespace PasteLoggerBoard.serilogmodels
{
    /// <summary>
    /// 日志消息info
    ///</summary>
    [TypeFilter(typeof(RoleAttribute), Arguments = new object[] { "data", "view" })]
    public class LogInfoAppService : PasteLoggerBoardAppService
    {

        private readonly IPasteLoggerBoardDbContext _dbContext;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbContext"></param>
        public LogInfoAppService(IPasteLoggerBoardDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PagedResultDto<LogInfoListDto>> Info(InputSearchLog input)
        {
            var query = _dbContext.LogInfo.Where(t => input.sdate <= t.DateLog && t.DateLog < input.edate && t.AppName == input.appname)
                .WhereIf(!String.IsNullOrEmpty(input.clientip), x => x.ClientIp == input.clientip)
                .WhereIf(!String.IsNullOrEmpty(input.requestid), x => x.RequestId == input.requestid)
                .WhereIf(!String.IsNullOrEmpty(input.connectionid), x => x.ConnectionId == input.connectionid)
                .WhereIf(!String.IsNullOrEmpty(input.path), x => x.Path == input.path)
                .WhereIf(!String.IsNullOrEmpty(input.word), x => x.MessageTemplate.Contains(input.word))
                .OrderBy(xy => xy.Id);
            var _pagedto = new PagedResultDto<LogInfoListDto>();
            if (input.page == 1) { _pagedto.TotalCount = await query.CountAsync(); }
            var userList = await query.Page(input.page, input.size).ToListAsync();
            var temList = ObjectMapper.Map<List<LogInfo>, List<LogInfoListDto>>(userList);
            _pagedto.Items = temList;
            return _pagedto;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PagedResultDto<LogErrorListDto>> Error(InputSearchLog input)
        {
            var query = _dbContext.LogError.Where(t => input.sdate <= t.DateLog && t.DateLog < input.edate && t.AppName == input.appname)
                                .WhereIf(!String.IsNullOrEmpty(input.clientip), x => x.ClientIp == input.clientip)
                .WhereIf(!String.IsNullOrEmpty(input.requestid), x => x.RequestId == input.requestid)
                .WhereIf(!String.IsNullOrEmpty(input.connectionid), x => x.ConnectionId == input.connectionid)
                                .WhereIf(!String.IsNullOrEmpty(input.path), x => x.Path == input.path)
                .WhereIf(!String.IsNullOrEmpty(input.word), x => x.MessageTemplate.Contains(input.word))
                .OrderBy(xy => xy.Id);
            var _pagedto = new PagedResultDto<LogErrorListDto>();
            if (input.page == 1) { _pagedto.TotalCount = await query.CountAsync(); }
            var userList = await query.Page(input.page, input.size).ToListAsync();
            var temList = ObjectMapper.Map<List<LogError>, List<LogErrorListDto>>(userList);
            _pagedto.Items = temList;
            return _pagedto;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PagedResultDto<LogExceptionListDto>> Exception(InputSearchLog input)
        {
            var query = _dbContext.LogException.Where(t => input.sdate <= t.DateLog && t.DateLog < input.edate && t.AppName == input.appname)
                                .WhereIf(!String.IsNullOrEmpty(input.clientip), x => x.ClientIp == input.clientip)
                .WhereIf(!String.IsNullOrEmpty(input.requestid), x => x.RequestId == input.requestid)
                .WhereIf(!String.IsNullOrEmpty(input.connectionid), x => x.ConnectionId == input.connectionid)
                                .WhereIf(!String.IsNullOrEmpty(input.path), x => x.Path == input.path)
                .WhereIf(!String.IsNullOrEmpty(input.word), x => x.MessageTemplate.Contains(input.word))
                .OrderBy(xy => xy.Id);
            var _pagedto = new PagedResultDto<LogExceptionListDto>();
            if (input.page == 1) { _pagedto.TotalCount = await query.CountAsync(); }
            var userList = await query.Page(input.page, input.size).ToListAsync();
            var temList = ObjectMapper.Map<List<LogException>, List<LogExceptionListDto>>(userList);
            _pagedto.Items = temList;
            return _pagedto;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PagedResultDto<LogWarningListDto>> Warning(InputSearchLog input)
        {
            var query = _dbContext.LogWarning.Where(t => input.sdate <= t.DateLog && t.DateLog < input.edate && t.AppName == input.appname)
                                .WhereIf(!String.IsNullOrEmpty(input.clientip), x => x.ClientIp == input.clientip)
                .WhereIf(!String.IsNullOrEmpty(input.requestid), x => x.RequestId == input.requestid)
                .WhereIf(!String.IsNullOrEmpty(input.connectionid), x => x.ConnectionId == input.connectionid)
                                .WhereIf(!String.IsNullOrEmpty(input.path), x => x.Path == input.path)
                .WhereIf(!String.IsNullOrEmpty(input.word), x => x.MessageTemplate.Contains(input.word))
                .OrderBy(xy => xy.Id);
            var _pagedto = new PagedResultDto<LogWarningListDto>();
            if (input.page == 1) { _pagedto.TotalCount = await query.CountAsync(); }
            var userList = await query.Page(input.page, input.size).ToListAsync();
            var temList = ObjectMapper.Map<List<LogWarning>, List<LogWarningListDto>>(userList);
            _pagedto.Items = temList;
            return _pagedto;
        }

    }
}
